package views.components;

import java.util.Locale;

import utils.MessageUtils;
import utils.SessionUtils;
//import views.SimpleLoginView;

import com.vaadin.server.ThemeResource;

//import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.NativeButton;

import config.Parameters;

@SuppressWarnings("serial")
/**
 * Component que et permitix seleccionar idioma
 *
 */
public class LangSelector extends CustomComponent implements
		NativeButton.ClickListener {

	private String btPrefix = "NativeButtonNum_";
	private String viewName;

	/**
	 * A button tool bar to select language or locale
	 */
	public LangSelector(String viewName) {

		this.viewName = viewName;
		GridLayout gl = new GridLayout(Parameters.LNG_ICO.length, 1);

		for (int i = 0; i < Parameters.LNG_ICO.length; i++) {
			NativeButton button = new NativeButton();
			button.setId(btPrefix + i);

			ThemeResource tr = new ThemeResource(Parameters.ICO_FLAGS48_PATH
					+ Parameters.LNG_ICO[i]);
			button.setIcon(tr);
			button.setDescription(MessageUtils
					.getI18n(Parameters.LNG_NAME[i]));
			button.addClickListener(this);
			gl.addComponent(button, i, 0);
			setCompositionRoot(gl);
			this.setCaption(MessageUtils.getI18n("language_select"));
		}

	}

	@Override
	public void buttonClick(ClickEvent event) {

		String btId = event.getButton().getId();
		int i = 0;
		try {
			i = Integer.parseInt(btId.replace(btPrefix, ""));
			SessionUtils.setSessionLocale(new Locale(Parameters.LNG_NAME[i]));
			this.getUI().getNavigator().navigateTo(viewName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
